package com.wwb.w0512;

import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MenuFragment extends Fragment {

    private View view;
    private int[] settingicon;
    private String[] citynames;
    private String[] settingText;
    private ListView mlistView;
    private FragmentManager manager;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_menu,container,false);
        MainActivity activity= (MainActivity) getActivity();
        settingicon=activity.getIcons();
        citynames=activity.getCitynames();
        settingText=activity.getSettingText();

       // manager=getSupportFragmentManager();

        if (view!=null){
            initView();
        }
        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ContentFragment listFragment = (ContentFragment) ((MainActivity)getActivity()).getSupportFragmentManager().findFragmentById(R.id.citycontent);
                //manager=getSupportFragmentManager();

                //Fragment fragment = (Fragment)getSupportFragmentManager().findFragmentById(R.id.citycontent);

                listFragment.setText(settingText[i]);
            }
        });
        return view;
    }

    private void initView() {
        mlistView=view.findViewById(R.id.menulist);
        if (settingicon!=null){
            mlistView.setAdapter(new MyAdapter());
        }
    }


    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return settingicon.length;
        }

        @Override
        public Object getItem(int i) {
            return settingicon[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View cview, ViewGroup viewGroup) {
            cview=View.inflate(getActivity(),R.layout.item_list,null);
            ImageView mNameTV=cview.findViewById(R.id.city_icon);
            mNameTV.setBackgroundResource(settingicon[i]);
            TextView mCityName=cview.findViewById(R.id.city_name);
            mCityName.setText(citynames[i]);

            return cview;
        }
    }





}